<?php
//判断是否正当访问
if(!defined('IN_DISCUZ') || empty($_G['formhash']) || empty($_G['uid']) || empty($_G['gp_tid']) || !submitcheck('activityapply')) {
	exit('Access Denied');
}

require_once DISCUZ_ROOT.'./source/plugin/donkey_activity/donkey_activity.func.php';
//判断活动状态
$activity = get_activity('stop');

//判断必要的表单域，这里只有一个applyid
if(empty($_G['gp_realname'])) {
	showmessage('请填写真实姓名');
}
if(empty($_G['gp_mobile'])) {
	showmessage('请填写手机号码');
}
if(empty($_G['gp_qq'])) {
	showmessage('请填写QQ号');
}

//获取表单参数，reason
$message = cutstr(dhtmlspecialchars($_G['gp_message']), 200);

$applyinfo = array();
$applyinfo = DB::fetch_first("SELECT * FROM ".DB::table('donkey_activity_apply')." WHERE tid='$_G[gp_tid]' AND uid='$_G[uid]'");
if($applyinfo && $applyinfo['verified'] < 2) {
	showmessage('不能重复申请');
}

$properties = array(
		'tid' => $_G['gp_tid'],
		'username' => $_G['username'],
		'uid' => $_G['uid'],
		'message' => $message,
		'verified' => ($activity['uid'] == $_G['uid'] ? 1 : 0),
		'dateline' => $_G['timestamp'],
		'payment' => ($_G['gp_payment'] ? intval($_G['gp_payvalue']) : -1),
		'ufielddata' => serialize(
			array(
				'realname'=>$_G['gp_realname'], 
				'mobile'=>$_G['gp_mobile'], 
				'qq'=>$_G['gp_qq']
				)
			)
	);
DB::insert('donkey_activity_apply', $properties);

showmessage('恭喜您，报名成功！');

//TODO, 报名人数
//$applynumber = DB::result_first("SELECT COUNT(*) FROM ".DB::table('donkey_activity_apply')." WHERE tid='$_G[gp_tid]' AND verified='1'");

if($activity['uid'] != $_G['uid']) {
	notification_add($activity['uid'], 'activity', 'activity_notice', array(
		'tid' => $_G['gp_tid'],
		'subject' => $thread['subject'],
	));
	$space = array();
	space_merge($space, 'field_home');

	if(!empty($space['privacy']['feed']['newreply'])) {
		$feed['icon'] = 'activity';
		$feed['title_template'] = 'feed_reply_activity_title';
		$feed['title_data'] = array(
			'subject' => "<a href=\"forum.php?mod=viewthread&tid=$_G[gp_tid]\">$thread[subject]</a>",
			'hash_data' => "tid{$_G[gp_tid]}"
		);
		$feed['id'] = $_G['gp_tid'];
		$feed['idtype'] = 'tid';
		postfeed($feed);
	}
}
showmessage('恭喜您，报名成功！', 'forum.php?mod=viewthread&tid='.$_G['gp_tid']);
?>